<?php

namespace app\api\controller\v1;

use addons\litestore\model\Litestoregoodsspec;
use addons\litestore\model\Wxlitestoregoods;
use app\admin\model\Chargeorder;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\litestore\Litestorenews;
use app\admin\model\Orderday;
use app\admin\model\Profitorder;
use app\admin\model\Profitorderundo;
use app\admin\model\Profitvip;
use app\admin\model\Publishwork;
use app\admin\model\Shopuser;
use app\admin\model\Useramount;
use app\admin\model\Userrelation;
use app\admin\model\Usersales;
use app\admin\model\Userweixin;
use app\admin\model\Workaddress;
use app\admin\model\Workhistory;
use app\admin\model\Worktype;
use app\api\service\LepayService;
use app\api\service\Orderservice;
use app\api\service\ProfitService;
use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 */
class Charge extends Api
{
    protected $noNeedLogin = ['callback'];
    protected $noNeedRight = ['*'];


    public function addOrder()
    {

        $user = $this->auth->getUser();
        $amount = $this->request->param('amount');


        $userWxModel = new Userweixin();
        $wx = $userWxModel->where('uid', $user['id'])->find();

        $orderNo = getOrderSn();
        $data = array();
        $data['desc'] = '用户充值';
        $data['openid'] = $wx['openid'];
        $data['orderNo'] = $orderNo;
        $data['amount'] = $amount*100;

        $data['pay_price'] = $amount*100;
        $data['callback_url'] = 'https://' . $_SERVER['HTTP_HOST'] . "/api/v1/charge/callback";
        $payService = new LepayService("3419916772", "");
        $ret = $payService->pay2($data);
        if ($ret['resp_code'] == 0) {


            $db = array();
            $db['uid'] = $user['id'];
            $db['createtime'] = time();
            $db['orderNo'] = $orderNo;
            $db['amount'] = $amount;

            $userChargeModel = new Chargeorder();
            $userChargeModel->insert($db);
            $info = array();
            $info['orderNo'] = $orderNo;
            $info['payInfo'] = json_decode($ret['jspay_info'], true);
            $this->success('下单成功', $info);
        }else{
            $this->error('下单失败');
        }
    }



    public function callback()
    {

        $param = file_get_contents("php://input");
        error_log("======agent pay=====callback=================" . $param);

        $jsonxml = json_encode(simplexml_load_string($param, 'SimpleXMLElement', LIBXML_NOCDATA));
        $result = json_decode($jsonxml, true);
        $orderNo = $result['third_order_id'];
        $status = $result['status'];

        if ($status == 2) {



            $chargeModel = new Chargeorder();
            $order = $chargeModel->where('orderNo',$orderNo)->find();
            if($order['status'] == 'paying'){

                $userModel = new \app\admin\model\User();
                $user = $userModel->where('id',$order['uid'])->find();
                $str = $user['username']." ****".substr($user['mobile'],8,4);
                $chargeModel->where('id',$order['id'])->update(['status'=>'payed']);
                $userAmountModel = new Useramount();
                $userAmountModel->insert(['uid'=>$order['uid'],'amount'=>$order['amount'],'type'=>4,'extra'=>$order['id'],'desc'=>$str,'createtime'=>time()]);
            }


        }
        echo "000000";


    }

}
